package cn.wp.ewallet.common.extension.authority;

import cn.wp.common.core.dao.SqlString;
import com.google.common.collect.Lists;

import java.util.List;

/**
 * 数据权限控制
 * Created by chenyb on 2019/12/25
 */
public class DataAuthority {

    private SqlString sqlString;

    private List<DataAuthorityStrategy> filterChain = Lists.newArrayList();

    private StringBuilder result = new StringBuilder();

    public DataAuthority(SqlString sqlString) {
        this.sqlString = sqlString;
        // 初始化策略处理链
        filterChain.add(new AuthorityGroupStrategy());
        filterChain.add(new AuthorityPmoStrategy());
        filterChain.add(new AuthorityUserStrategy());
    }

    ;

    /**
     * 自定义处理策略链
     *
     * @param sqlString
     * @param filterChain
     */
    public DataAuthority(SqlString sqlString, List<DataAuthorityStrategy> filterChain) {
        this.sqlString = sqlString;
        this.filterChain = filterChain;
    }

    ;

    /**
     * 执行过滤
     *
     * @return
     */
    public String filterSql() {
        /*
        AuthenticationInfo authenticationInfo = AuthenticationContext.self().currentUser();
        List<String> roleIdList = authenticationInfo.getRoleIds();

        // 测试
       /* AuthenticationInfo authenticationInfo = new AuthenticationInfo();
        authenticationInfo.setId("1005");
        authenticationInfo.setOrgId("ROOT");
        List<String> roleIdList = Lists.newArrayList();
        roleIdList.add("1000");*/
        //roleIdList.add("2000");
        //roleIdList.add("3000");

        StringBuilder result = new StringBuilder();
        /*
        result.append("select * from (" + sqlString + ") _ftbl ");

        int index = 0;
        StringBuilder where = new StringBuilder();
        for(String roleId : roleIdList){
            for(DataAuthorityStrategy strategy : filterChain){
                if(strategy.support(roleId)){
                    String condition = strategy.filter(sqlString.toString(),authenticationInfo);
                    if(StrUtil.isBlank(condition)) {
                        break;
                    }

                    if(index == 0){
                        where.append(" where ");
                        where.append("(");
                    }
                    if(index > 0){
                        where.append(" or ");
                    }

                    where.append(condition);
                    index++;
                    break;
                }
            }
        }

        if(where.length() > 0){
            result.append( where + ")");
        }


*/
        return result.toString();
    }

    public static void main(String[] args) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select a.*, b.ORG_ID");
        sqlString.add("from staff_info a, STAFF_ORG_REL b, ORGANIZATION_INFO c");
        sqlString.add("where a.STAFF_ID = b.STAFF_ID");
        sqlString.add("    and b.ORG_ID = c.ORG_ID");
        DataAuthority dataAuthority = new DataAuthority(sqlString);
        System.err.println(dataAuthority.filterSql());
    }
}
